package com.cqjtu.tto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqjtu.tto.dao.mapper.DepartmentMapper;
import com.cqjtu.tto.dao.mapper.ProjectMapper;
import com.cqjtu.tto.dao.mapper.ProStageMapper;
import com.cqjtu.tto.dao.mapper.StageMapper;
import com.cqjtu.tto.dao.pojo.Department;
import com.cqjtu.tto.dao.pojo.ProjectStage;
import com.cqjtu.tto.dao.pojo.Project;
import com.cqjtu.tto.service.FolderService;
import com.cqjtu.tto.service.ProjectService;
import com.cqjtu.tto.utils.NumUtil;
import com.cqjtu.tto.vo.ErrorCode;
import com.cqjtu.tto.vo.Results;
import com.cqjtu.tto.vo.params.ProAddParam;
import com.cqjtu.tto.vo.params.stages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.PrinterJob;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    ProStageMapper proStageMapper;

    @Autowired
    StageMapper stageMapper;

    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    FolderService folderService;


    @Override
    public Results addProject(ProAddParam proAddParam) throws InterruptedException {                                             //用于项目非空数据的添加
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_name",proAddParam.getProjectName());
        Project project = projectMapper.selectOne(queryWrapper);
        System.out.println(project);
        if(project == null || project.getProjectDeleted() == 1){
            project = new Project(proAddParam);
            project.setProjectId(NumUtil.nextPkId());

//            queryWrapper.clear();
//            queryWrapper.eq("department_name","后期部1");
//            Department department = departmentMapper.selectOne(queryWrapper);
            queryWrapper.clear();
            queryWrapper.eq("stage_name","模型");
            stages stages = stageMapper.selectOne(queryWrapper);
//            stages stage = new stages(NumUtil.nextPkId(),department.getDepartmentId(),
//                    department.getDepartmentResponsibleContent(),"正在进行后期制作",0);                    //初始化添加阶段信息
            ProjectStage proStage = new ProjectStage(NumUtil.nextPkId(),project.getProjectId(),
                    stages.getStageId(),null,null,
                    stages.getStageDescription(),project.getProjectVersion()
                    ,null,null,0);                               //初始化添加项目——阶段信息
            project.setProjectStatus(stages.getStageDescription());
            try {
//                stageMapper.insert(stage);
                projectMapper.insert(project);
                proStageMapper.insert(proStage);
                folderService.addFold(project);

            } catch (Exception e) {
                e.printStackTrace();
                return Results.fail(ErrorCode.PARAMS_ERROR.getCode(), "阶段信息添加失败！");
            }
            return Results.success("项目信息增加成功！");

        }
         return Results.fail(ErrorCode.ROLE_EXIST.getCode(),"该项目已经存在！");
    }


    @Override
    public Results delProject(Long pro_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_id",pro_id);
        Project project = projectMapper.selectOne(queryWrapper);
        if(project == null || project.getProjectDeleted() == 1){
            return Results.fail(ErrorCode.USER_NOT_EXIST.getCode(),"该项目信息不存在！");
        }
        project.setProjectDeleted(1);
        if (projectMapper.updateById(project) > 0) {
            return Results.success("项目删除成功！");
        }
        return Results.fail(-500,"系统错误！");
    }

    @Override
    public Results CusEvaluate(Long pro_id,String evaluation) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_id",pro_id);
        Project project = projectMapper.selectOne(queryWrapper);
        if(project == null || project.getProjectDeleted() == 1){
            return Results.fail(ErrorCode.USER_NOT_EXIST.getCode(),"该项目信息不存在！");
        }
        project.setCustomerEvaluation(evaluation);
        if (projectMapper.updateById(project) > 0) {
            return Results.success("更新成功！");
        }
        return Results.fail(-500,"系统错误");
    }

    @Override
    public Results proselectOne(String pro_name) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_name",pro_name);
        Project project = projectMapper.selectOne(queryWrapper);
        if(project == null || project.getProjectDeleted() == 1){
            return Results.fail(ErrorCode.USER_NOT_EXIST.getCode(),"该项目信息不存在！");
        }
        return Results.success(project);
    }

    @Override
    public Results proselectAllEnd(Long user_id) {                        //已经结束的项目
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user_id);
        queryWrapper.eq("project_deleted",1);
        List<Project> projects = projectMapper.selectList(queryWrapper);
        if (projects.isEmpty()) {
            return Results.fail(ErrorCode.USER_NOT_EXIST.getCode(),"不存在任何项目信息！");
        }
        return Results.success(projects);
    }

    @Override
    public Results proselectAllnow(Long user_id) {                        //未结束的项目
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user_id);
        queryWrapper.eq("project_deleted",0);
        List<Project> projects = projectMapper.selectList(queryWrapper);
        if (projects.isEmpty()) {
            return Results.fail(ErrorCode.USER_NOT_EXIST.getCode(),"不存在任何项目信息！");
        }
        return Results.success(projects);
    }

    @Override
    public Results StageEvaluate(Long pro_id,Long stage_id, String evaluation, Integer score) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_id",pro_id);
        queryWrapper.eq("stage_id",stage_id);
        ProjectStage projectStage = proStageMapper.selectOne(queryWrapper);
        if(projectStage == null ||projectStage.getProjectStageDeleted() == 1){
            return Results.fail(-200,"不存在该项目阶段！");
        }
        projectStage.setStageEvaluation(evaluation);
        projectStage.setStageScore(score);
        if (proStageMapper.updateById(projectStage) > 0) {
            return Results.success("阶段评价成功！");
        }
        return Results.fail(-500,"系统错误！");
    }

    @Override
    public Results StageDel(Long pro_stage_id) {
        ProjectStage projectStage = proStageMapper.selectById(pro_stage_id);
        if(projectStage == null ||projectStage.getProjectStageDeleted() == 1){
            return Results.fail(-200,"不存在该项目阶段！");
        }
        projectStage.setProjectStageDeleted(1);
        if (proStageMapper.updateById(projectStage) > 0) {
            return Results.success("阶段删除成功！");
        }
        return Results.fail(-500,"系统错误！");
    }

    @Override
    public Results StageSelectOne(Long pro_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_id",pro_id);
        queryWrapper.eq("project_stage_deleted",1);
        ProjectStage projectStage = proStageMapper.selectOne(queryWrapper);
        if(projectStage == null){
            return Results.fail(-200,"不存在项目阶段！");
        }
        return Results.success(projectStage);
    }

    @Override
    public Results NextStage(Long pro_id) throws InterruptedException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_id",pro_id);
        queryWrapper.eq("project_stage_deleted",0);                               //获取当前项目阶段信息
        ProjectStage projectStage = proStageMapper.selectOne(queryWrapper);
        String LastStage = projectStage.getStageStatus().substring(2,4);
        projectStage.setProjectStageDeleted(1);
        queryWrapper.clear();
        queryWrapper.eq("stage_id",projectStage.getStageId()+1);                  //获取下一个阶段信息
        stages stages = stageMapper.selectOne(queryWrapper);
        if(stages == null){
            return Results.fail(-500,"当前为项目最后阶段！");
        }
        ProjectStage proStage = new ProjectStage(NumUtil.nextPkId(),pro_id,
                stages.getStageId(),null,null,
                stages.getStageDescription(),projectStage.getStatusVersion()
                ,null,null,0);
        Project project = projectMapper.selectById(pro_id);
        project.setProjectStatus(stages.getStageDescription());
        if (proStageMapper.insert(proStage) > 0) {
            projectStage.setStageStatus(LastStage+"阶段已结束！");
            proStageMapper.updateById(projectStage);
            projectMapper.updateById(project);
            return Results.success("成功开始"+stages.getStageName()+"阶段！");
        }
        return Results.fail(-500,"系统错误");
    }

    @Override
    public Results EndProject(Long pro_id,Float pro_income,String evaluation) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_id",pro_id);
        queryWrapper.eq("project_stage_deleted",0);                               //获取当前项目阶段信息
        ProjectStage projectStage = proStageMapper.selectOne(queryWrapper);
        if(projectStage == null){
            return Results.fail(-500,"未查找到当前项目阶段信息！");
        }
        queryWrapper.clear();
        queryWrapper.eq("stage_id", projectStage.getStageId());
        stages stages = stageMapper.selectOne(queryWrapper);
        if (!stages.getStageName().equals("后期")) {
            return Results.fail(-500,"项目未进行到最后阶段,无法结束！");
        }
        String LastStage = projectStage.getStageStatus().substring(2,4);
        projectStage.setProjectStageDeleted(1);
        projectStage.setStageStatus(LastStage+"阶段已结束！");
        proStageMapper.updateById(projectStage);                                              //更新阶段信息
        queryWrapper.clear();
        queryWrapper.eq("project_id",pro_id);
        queryWrapper.eq("project_deleted",0);
        Project project = projectMapper.selectOne(queryWrapper);
        if(project == null){
            return Results.fail(-500,"未查找到该项目信息！");
        }
        project.setProjectIncome(pro_income);
        project.setCustomerEvaluation(evaluation);
        project.setProjectStatus("项目已结束！");
        project.setProjectDeleted(1);
        if (projectMapper.updateById(project) > 0) {
            return Results.success("项目已成功结束！");
        }
        return Results.fail(-500,"系统错误！");
    }

    @Override
    public Results BackProject(Long pro_id, String stage_name) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("stage_name",stage_name);
        stages stages = stageMapper.selectOne(queryWrapper);
        queryWrapper.clear();
        queryWrapper.eq("project_id",pro_id);
        List<ProjectStage> list = proStageMapper.selectList(queryWrapper);
        int flag=0;
        for (ProjectStage projectStage : list) {                                               //将回退之前的阶段保存，删除之后的阶段
            if(flag == 1)
            {
                queryWrapper.clear();
                queryWrapper.eq("project_id",projectStage.getProjectId());
                queryWrapper.eq("stage_id",projectStage.getStageId());
                proStageMapper.delete(queryWrapper);
            }
            if(projectStage.getStageId().longValue()==stages.getStageId().longValue()){
                flag = 1;
            }
        }
        queryWrapper.clear();
        queryWrapper.eq("project_id",pro_id);
        queryWrapper.eq("stage_id",stages.getStageId());
        ProjectStage projectStage = proStageMapper.selectOne(queryWrapper);               //修改回退至的阶段信息
        projectStage.setStageStatus(stages.getStageDescription());
        projectStage.setProjectStageDeleted(0);
        proStageMapper.updateById(projectStage);
        queryWrapper.clear();
        Project project = projectMapper.selectById(pro_id);
        project.setProjectStatus("项目正在进行");
        project.setProjectIncome(0);
        project.setCustomerEvaluation("");
        project.setProjectDeleted(0);
        if (projectMapper.updateById(project) > 0) {
            return Results.success("项目回退成功！");
        }
        return Results.fail(-500,"系统错误");
    }

    @Override
    public Results proStageNow(Long project_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_id",project_id);
        queryWrapper.eq("project_stage_deleted",0);
        ProjectStage projectStage = proStageMapper.selectOne(queryWrapper);
        if(projectStage == null){
            return Results.fail(ErrorCode.USER_NOT_EXIST.getCode(), "该项目阶段信息不存在！");
        }
        return Results.success(projectStage);
    }

    @Override
    public Results proAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_deleted",0);
        List list = projectMapper.selectList(queryWrapper);
        if (!list.isEmpty()) {
            return Results.success(list);
        }
        return Results.fail(-500,"当前未含有任何项目！");
    }
}
